package org.easyx.ui.controller.oa;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.easyx.core.ui.easyui.PageBean;
import org.easyx.core.ui.easyui.PageParam;
import org.easyx.core.utils.ObjKit;
import org.easyx.ser.oa.entity.Notices;
import org.easyx.ser.oa.service.NoticesService;
import org.easyx.ui.base.BossEasyUIBaseController;
import org.easyx.ui.base.BossEasyUISessionUser;
import org.easyx.ui.util.SessionKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * <p>
 * 信息公告表 前端控制器
 * </p>
 *
 * @author xiaojl
 * @since 2017-10-10
 */
@Controller
@RequestMapping("/info/notices")
public class NoticesController extends BossEasyUIBaseController {
	private static final Logger log = LoggerFactory.getLogger(NoticesController.class);
	private static final String basePagePath = "info/notices/";
	
	@Autowired
	private NoticesService noticesService;
	
	@RequestMapping("/top5Notices")
	public String top5Notices(){
		Page<Notices> page = noticesService.selectPage(new Page<Notices>(1,5),
				new EntityWrapper<Notices>().orderBy("createTime", false));
		
		request.setAttribute("list", page.getRecords());
		request.setAttribute("size", page.getRecords().size());
		return "framework/easyuiRigthNotices";
	}
	/**
	 * 进入列表页面
	 */
	@RequiresPermissions("info/notices/manager")
	@RequestMapping("/manager")
	public String manager(Model model){
		BossEasyUISessionUser su = SessionKit.getSessionUser(request);
		model.addAttribute("sessionUser", su);
		
		return basePagePath+"list";
	}
	
	/**
	 * 查询列表数据(分页)
	 */
	@RequiresPermissions("info/notices/manager")
	@RequestMapping("/dataGrid")
	public @ResponseBody Object dataGrid(PageParam pageParam, Notices entity){
		PageBean<Notices> pageBean = this.selectListPage(pageParam, entity, noticesService);
		return pageBean;
	}
	
	/**
	 * 转到添加页面 
	 */
	@RequiresPermissions("info/notices/add")
	@RequestMapping("/addPage")
	public String addPage(){
		return basePagePath+"add";
	}
	
	/**
	 * 保存 .
	 */
	@RequiresPermissions("info/notices/add")
	@RequestMapping("/add")
	public @ResponseBody Object add(@Valid Notices entity, BindingResult result) {
		try {
			if(result.hasErrors()){
				return operateError(result.getAllErrors().get(0).getDefaultMessage());
			}
			
			/** 数据唯一性校验 */
			
			
			addOptions(entity);
			//插入数据库
			noticesService.insert(entity);
			
			return operateSuccess("添加成功!");
		} catch (Exception e) {
			log.error("保存添加的数据，出现异常:", e);
			return operateError("添加失败!");
		}
	}
	
	/**
	 * 转到详情页面 .
	 */
	@RequiresPermissions("info/notices/detail")
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		try {
			Notices entity = noticesService.selectById(id);
			if (ObjKit.isEmpty(entity)) {
				log.error("进入详情页面时，获取想要修改的数据为空！id="+id);
			}

			model.addAttribute("entity",entity);
		} catch (Exception e) {
			log.error("进入详情页面时，获取想要修改的数据出现异常:id="+id, e);
		}
		return  basePagePath+"detail" ;
	}
	
	/**
	 * 转到修改页面 .
	 */
	@RequiresPermissions("info/notices/edit")
	@RequestMapping("/editPage")
	public String editPage(Model model, Long id) {
		try {
			Notices entity = noticesService.selectById(id);
			if (ObjKit.isEmpty(entity)) {
				log.error("进入编辑页面时，获取想要修改的数据为空！id="+id);
			}

			model.addAttribute("entity",entity);
		} catch (Exception e) {
			log.error("进入编辑页面时，获取想要修改的数据出现异常:id="+id, e);
		}
		return  basePagePath+"edit" ;
	}
	
	/**
	 * 保存修改后的信息 .
	 */
	@RequiresPermissions("info/notices/edit")
	@RequestMapping("/edit")
	public @ResponseBody Object edit(@Valid Notices entity, BindingResult result) {
		try {
			if(result.hasErrors()){
				return operateError(result.getAllErrors().get(0).getDefaultMessage());
			}
			
			Long id = entity.getId();
			Notices oldObj = noticesService.selectById(id);
			if (ObjKit.isEmpty(oldObj)) {
				return operateError("保存修改的数据时，无法获取想要修改的数据，id="+id);
			}
			
			/** 数据唯一性校验逻辑*/
			
			
			//更新数据库
			noticesService.updateById(entity);
			
			return operateSuccess("修改成功!");
		} catch (Exception e) {
			log.error("保存修改的数据时，出现异常:", e);
			return operateError("修改失败!");
		}
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("info/notices/delete")
	@RequestMapping("/delete")
	public @ResponseBody Object delete(Long id) {
		try {
			Notices entity = noticesService.selectById(id);
			if (ObjKit.isEmpty(entity)) {
				return operateError("删除数据时，无法获取想要删除的数据，id="+id);
			}
			
			noticesService.deleteById(id);
			
			return operateSuccess("删除成功!");
		} catch (Exception e) {
			log.error("删除数据时，出现异常:id="+id, e);
			return operateError("删除失败!");
		}
	}
	
}
